package com.zj.rocketmq.demo.listenner;

import com.alibaba.fastjson.JSONObject;
import com.zj.rocketmq.demo.entity.TOrder;
import com.zj.rocketmq.demo.service.GoodsService;
import com.zj.rocketmq.demo.service.TorderService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "scramble-topic",consumerGroup =
        "scramble-group-1")
public class RocketMqListenner implements RocketMQListener<String> {
    @Autowired
    private TorderService torderService;
    @Autowired
    private GoodsService goodsService;
    @Override
    public void onMessage(String s) {
        TOrder tOrder = JSONObject.parseObject(s, TOrder.class);
        if(goodsService.haveStock(tOrder.getGoodsId())){
            goodsService.decrement(tOrder.getGoodsId());
            tOrder.setStatus("成功");
            torderService.save(tOrder);
        }
    }
}
